package com.yanqu.road.web.controller.game;

import com.alibaba.fastjson.JSONObject;
import com.yanqu.road.entity.web.HttpJsonWebParam;
import com.yanqu.road.logic.config.ChannelConfig;
import com.yanqu.road.server.helper.SignHelper;
import com.yanqu.road.server.manger.AppKeyMgr;
import com.yanqu.road.server.manger.ServerListMgr;
import com.yanqu.road.web.WebServer;
import com.yanqu.road.web.controller.BaseJsonHttpServlet;
import com.yanqu.road.webservice.invoke.GameWebServiceInvoke;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.SortedMap;

@WebServlet(urlPatterns = "/reachPlatformRewardCondition")
public class ReachPlatformRewardCondition extends BaseJsonHttpServlet {


    private static Logger logger = LogManager.getLogger(ReachPlatformRewardCondition.class.getName());

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        logger.info("request reachPlatformRewardCondition");
        HttpJsonWebParam httpJsonWebParam = super.parseRequest(request);
        JSONObject paramObj = httpJsonWebParam.getParamObj();
        long userId;
        int type;
        String sign;
        try{
            userId = paramObj.getLongValue("role_id");
            type = paramObj.getIntValue("type");
            sign = paramObj.getString("sign");
        } catch (Exception e) {
            logger.error("reachPlatformRewardCondition args error:" + e);
            sendResult(response, 0, "args error");
            return;
        }
        try{
            SortedMap<String, String> paramMap = SignHelper.getParamMap(paramObj);
            if(!SignHelper.checkSign(paramMap, AppKeyMgr.getAndroidAppSecret(), sign)){
                if(ServerListMgr.isCnMergeServer(WebServer.getInstance().getServerId())){
                    if(!SignHelper.checkSign(paramMap, ChannelConfig.APP_SECRET_CONFIG, sign)){
                        logger.error("reachPlatformRewardCondition sign error");
                        sendResult(response, 0, "sign error");
                        return;
                    }
                }else {
                    logger.error("reachPlatformRewardCondition sign error");
                    sendResult(response, 0, "sign error");
                    return;
                }
            }
            int code = GameWebServiceInvoke.reachPlatformCondition(userId, type);
            JSONObject resultObj;
            if(code == 1) {
                resultObj = getResultObj(code, "success");
            }else {
                resultObj = getResultObj(code, "error");
            }
            sendMessage(response, resultObj.toJSONString());
        } catch (Exception e) {
            logger.error("reachPlatformRewardCondition error:" + e);
            sendResult(response, 0, "other error");
        }
    }

}
